package com.teama4.shop.controller;

import com.teama4.shop.annotation.NeedLogin;
import com.teama4.shop.domain.User;
import com.teama4.shop.service.CategoryService;
import com.teama4.shop.service.UserService;
import com.teama4.shop.utils.FileUtil;
import com.teama4.shop.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.UUID;


@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 上传图片
     *
     * @param type
     * @param file
     */
    @NeedLogin
    @RequestMapping("uploadPic")
    public void uppload(HttpServletResponse resp, HttpServletRequest req, String type, MultipartFile file) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession(false);

        if (session != null && session.getAttribute("userId") != null) {
            Integer userId = (Integer) session.getAttribute("userId");


            String filePath = null;
            boolean res = false;//数据库更新结果
            boolean saveFileRes = false;//保存数据结果


            if (type.equals("UserFrontOfIdCard")) {
                filePath = "/myFile/img/userIdCardPic";
                String fileName;
                User userT = userService.selectByPrimaryKey(userId);
                //如果数据库中有数据
                if (userT.getFrontOfIdCard() != null && !userT.getFrontOfIdCard().equals("")) {
                    fileName = userT.getFrontOfIdCard();
                    if (FileUtil.dictionary_add(file, filePath, fileName)) {
                        saveFileRes = true;
                        res = true;
                    }

                } else {

                    String oldFilename = file.getOriginalFilename();
                    String sname = oldFilename.substring(oldFilename.lastIndexOf("."));
                    fileName = UUID.randomUUID() + sname;

                    User user = new User();
                    user.setId(userId);
                    user.setFrontOfIdCard(fileName);
                    if (userService.updateByPrimaryKeySelective(user) > 0) {
                        //数据库更新成功
                        res = true;
                        if (FileUtil.dictionary_add(file, filePath, fileName)) {
                            saveFileRes = true;
                        }
                    }
                }


            } else if (type.equals("UserBackOfIdCardUpload")) {
                filePath = "/myFile/img/userIdCardPic";
                String fileName;
                User userT = userService.selectByPrimaryKey(userId);
                //如果数据库中有数据
                if (userT.getBackOfIdCard() != null && !userT.getBackOfIdCard().equals("")) {
                    fileName = userT.getBackOfIdCard();
                    if (FileUtil.dictionary_add(file, filePath, fileName)) {
                        saveFileRes = true;
                        res = true;
                    }

                } else {
                    String oldFilename = file.getOriginalFilename();
                    String sname = oldFilename.substring(oldFilename.lastIndexOf("."));
                    fileName = UUID.randomUUID() + sname;
                    User user = new User();
                    user.setId(userId);
                    user.setBackOfIdCard(fileName);
                    if (userService.updateByPrimaryKeySelective(user) > 0) {
                        //数据库更新成功
                        res = true;
                        if (FileUtil.dictionary_add(file, filePath, fileName)) {
                            saveFileRes = true;
                        }
                    }
                }
            } else if (type.equals("userHead")) {
                filePath = "/myFile/img/userHeadPic";
                String fileName;
                User userT = userService.selectByPrimaryKey(userId);
                //如果数据库中有数据
                if (userT.getHead() != null && !userT.getHead().equals("")) {
                    fileName = userT.getHead();
                    if (FileUtil.dictionary_add(file, filePath, fileName)) {
                        saveFileRes = true;
                        res = true;
                    }

                } else {
                    String oldFilename = file.getOriginalFilename();
                    String sname = oldFilename.substring(oldFilename.lastIndexOf("."));
                    fileName = UUID.randomUUID() + sname;
                    User user = new User();
                    user.setId(userId);
                    user.setHead(fileName);
                    if (userService.updateByPrimaryKeySelective(user) > 0) {
                        //数据库更新成功
                        res = true;
                        if (FileUtil.dictionary_add(file, filePath, fileName)) {
                            saveFileRes = true;
                        }
                    }
                }
            }


            if (saveFileRes) {
                if (res) {
                    //如果更新成功
                    res = false;
                    saveFileRes = false;
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果更新失败
                    res = false;
                    saveFileRes = false;
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);

                }
            } else {
                //如果文件保存失败
                res = false;
                saveFileRes = false;
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "saveFail");
                JSONUtil.printByJSON(resp, map);
            }

        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }

    }


    /**
     * 上传图片
     *
     * @param file
     */
    @NeedLogin
    @RequestMapping("upploadEvaPic")
    public void upploadEvaPic(HttpServletResponse resp, HttpServletRequest req, Integer orderId, Integer specificationId, MultipartFile file) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession(false);

        if (session != null && session.getAttribute("userId") != null) {
            Integer userId = (Integer) session.getAttribute("userId");


            String filePath = null;
            boolean res = false;//数据库更新结果
            boolean saveFileRes = false;//保存数据结果

            filePath = "/myFile/img/evaluatePic";
            String fileName;

            String oldFilename = file.getOriginalFilename();
            String sname = oldFilename.substring(oldFilename.lastIndexOf("."));
            fileName = UUID.randomUUID() + sname;

            User user = new User();
            user.setId(userId);
            user.setFrontOfIdCard(fileName);
            if (userService.updateByPrimaryKeySelective(user) > 0) {
                //数据库更新成功
                res = true;
                if (FileUtil.dictionary_add(file, filePath, fileName)) {
                    saveFileRes = true;
                }
            }


            if (saveFileRes) {
                if (res) {
                    //如果更新成功
                    res = false;
                    saveFileRes = false;
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    map.put("fileName",fileName);
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果更新失败
                    res = false;
                    saveFileRes = false;
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);

                }
            } else {
                //如果文件保存失败
                res = false;
                saveFileRes = false;
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "saveFail");
                JSONUtil.printByJSON(resp, map);
            }

        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }

    }


}
